<!--#include file="lib/utf-8.asp"-->
<!--#include file="lib/connopen.asp"-->
<!--#include file="lib/asp-common-lib.asp"-->
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>客户关系管理及报关单统计系统</title>
<!--#include file="lib/plugins.asp"-->
<!--#include file="lib/js-jquery-validate-lib.asp"-->
<!--#include file="lib/js-common-lib.asp"-->

<style type="text/css">
	.ui-datepicker table {
		border: 0;
	}
	tbody th{
		text-align:center;
	}
	#tmp{
		font-family:宋体;
	}
	.formError .formErrorContent{
		font-size:12px;
		width:100%
	}
</style>
<script type="text/javascript">
	$(function() {
		$("#form1").validationEngine();
		$(".datepicker").datepicker();
		$("#print_process").click(function(){
			window.open('process_print.asp?id='+$("#id").val(), '_blank', 'height=768px, width=1024px, scrollbars=yes, resizable=yes');
		});
		
		var json_tmp;
		$("#chk_detail").attr("checked") ? $("#detail").show() : $("#detail").hide()
		$("#chk_detail").click(function(){
			$(this).attr("checked") ? $("#detail").show() : $("#detail").hide()
		});
		
		$("#form1").keypress(function(e) {
			if (e.which == 13 || e.keyCode==13 ) // 判断所按是否回车键
			{
				//e.keyCode=9;
				return false;// 取消默认的提交行为
			}
		});
		$("#detail input:text").keypress(function(e) {
			if (e.which == 13 || e.keyCode==13 ) // 判断所按是否回车键
			{
				var inputs = $("#detail").find(":text");                   //获取表单中的所有输入框
				var idx = inputs.index(this);      //获取当前焦点输入框所处的位置
				//if (idx == inputs.length - 1)     // 判断是否是最后一个输入框
				//{
				//		if (confirm("最后一个输入框已经输入,是否提交?"))   //用户确认
				//				$("#myform").submit();    //提交表单
				//}
				//else{
					inputs[idx + 1].focus();       //设置焦点
					inputs[idx + 1].select();       //选中文字
				//}
				//return false;// 取消默认的提交行为
			}
		});

		$.getJSON("json_tmp.asp",function(data){
			json_tmp=data;
			var tmp_option="";
			$.each(data,function(i,item){
				tmp_option+='<option value="'+item.id+'">'+FillSpace(item.id,10)+FillSpace(item.receive,10)+item.pay+'</option>';
			});
			$("#tmp").append(tmp_option);
			$("#tmp").attr("value",$("#wtr").find("option:selected").attr("title"));
			if($("#form1 input[name=submit]").val()=="新增"){
				$("#tmp").trigger('change');
			}
		});
		$("#tmp").change(function(){
			find_tmp=false;
			tmp_id=$(this).val();
			$.each(json_tmp,function(i,item){
				if(item.id==tmp_id){
					$("#cd_receive").val(item.receive);
					$("#cd_pay").val(item.pay);
					$("#total_receive").val(calc_total_receive());
					$("#total_pay").val(calc_total_pay());
					find_tmp=true;
					return false;
				}
			});
			if(!find_tmp){
				$("#cd_receive").val("");
				$("#cd_pay").val("");
				$("#total_receive").val(calc_total_receive());
				$("#total_pay").val(calc_total_pay());
			}
		});
		$("#wtr").change(function(){
			//alert($(this).find("option:selected").attr("title"));
			$("#tmp").attr("value",$(this).find("option:selected").attr("title"));
			$("#tmp").trigger('change');
		});
		$("input[name=cd_status]").click(function(){
			$("#vote2,#vote3").next().next().html("");
			$(this).next().next().html((new Date()).toLocaleDateString());
		});
		function isnumber(s){
			s=$.trim(s);
			if(isNaN(s)||s==""){
				return false;
			}else{
				return true;
			}
		}
		function tonumber(s){
			if(isnumber(s)){
				s=parseFloat(s);
			}else{
				s=0;
			}
			return s;
		}
		function calc_total_receive(){
			var cd_receive,cy_receive,sj_receive,jk_receive,qt_receive;
			cd_receive=tonumber($.trim($("#cd_receive").val()));
			cy_receive=tonumber($.trim($("#cy_receive").html()));
			sj_receive=tonumber($.trim($("#sj_receive").html()));
			jk_receive=tonumber($.trim($("#jk_receive").html()));
			qt_receive=tonumber($.trim($("#qt_receive").html()));
			return cd_receive+cy_receive+sj_receive+jk_receive+qt_receive;
		}
		function calc_total_pay(){
			var cd_pay,cy_pay,sj_pay,jk_pay,qt_pay;
			cd_pay=tonumber($.trim($("#cd_pay").val()));
			cy_pay=tonumber($.trim($("#cy_pay").html()));
			sj_pay=tonumber($.trim($("#sj_pay").html()));
			jk_pay=tonumber($.trim($("#jk_pay").html()));
			qt_pay=tonumber($.trim($("#qt_pay").html()));
			return cd_pay+cy_pay+sj_pay+jk_pay+qt_pay;
		}
		//$("#total_receive").val(calc_total_receive());
		//$("#total_pay").val(calc_total_pay());
		$("#cd_receive,#cd_pay").keyup(function(){
			$("#total_receive").val(calc_total_receive());
			$("#total_pay").val(calc_total_pay());
		});
		
		function calc_detail(i){
			var t=0;
			$("#detail tr td:nth-child("+i+")>input").each(function(){
				v=$.trim($(this).val());
				if(isnumber(v)){
					v=parseFloat(v);
				}else{
					v=0;
				}
				t+=v;
			});
			return t;
		}
		$("#cy_receive").html(calc_detail(2));
		$("#cy_pay").html(calc_detail(3));
		$("#sj_receive").html(calc_detail(5));
		$("#sj_pay").html(calc_detail(6));
		$("#jk_receive").html(calc_detail(8));
		$("#jk_pay").html(calc_detail(9));
		$("#qt_receive").html(calc_detail(11));
		$("#qt_pay").html(calc_detail(12));
		$("#detail tr td:nth-child(2)>input").keyup(function(){
			$("#cy_receive").html(calc_detail(2));
			$("#total_receive").val(calc_total_receive());
		});
		$("#detail tr td:nth-child(3)>input").keyup(function(){
			$("#cy_pay").html(calc_detail(3));
			$("#total_pay").val(calc_total_pay());
		});
		$("#detail tr td:nth-child(5)>input").keyup(function(){
			$("#sj_receive").html(calc_detail(5));
			$("#total_receive").val(calc_total_receive());
		});
		$("#detail tr td:nth-child(6)>input").keyup(function(){
			$("#sj_pay").html(calc_detail(6));
			$("#total_pay").val(calc_total_pay());
		});
		$("#detail tr td:nth-child(8)>input").keyup(function(){
			$("#jk_receive").html(calc_detail(8));
			$("#total_receive").val(calc_total_receive());
		});
		$("#detail tr td:nth-child(9)>input").keyup(function(){
			$("#jk_pay").html(calc_detail(9));
			$("#total_pay").val(calc_total_pay());
		});
		$("#detail tr td:nth-child(11)>input").keyup(function(){
			$("#qt_receive").html(calc_detail(11));
			$("#total_receive").val(calc_total_receive());
		});
		$("#detail tr td:nth-child(12)>input").keyup(function(){
			$("#qt_pay").html(calc_detail(12));
			$("#total_pay").val(calc_total_pay());
		});
	});
</script>	
<!--#include file="lib/js-google-analytics.asp"-->
</head>
<%
id=request("id")
submit=request("submit")
if id="" then
	'初始化空白单
	action="add"
	'初始化日期
	cd_date=date()
	'初始化岗位
	cd_post=1
else
 action="edit"
end if
' bt_act=request("bt_act")
' if bt_act<>"" then
	' cd_actreceive=request("cd_actreceive")
	' cd_actpay=request("cd_actpay")
	' if bt_act="已收" then sql_act="update cd set cd_received=true,cd_actreceive="&cd_actreceive&" where id="&id
	' if bt_act="已付" then sql_act="update cd set cd_paid=true,cd_actpay="&cd_actpay&" where id="&id
	' call ExecuteCommand(sql_act,false)
' end if

if submit<>"" then
	action=request("action")
	'事务开始
	conn.BeginTrans
	on error resume next
	wtr=request("wtr")
	bwtr=request("bwtr")
	category=request("category")
	cd_no=request("cd_no")
	cd_date=request("cd_date")
	cd_company=trim(request("cd_company"))
	cd_voyage=UCase(trim(request("cd_voyage")))
	cd_lading=UCase(trim(request("cd_lading")))
	cd_verify=request("cd_verify")
	cd_status=request("cd_status")
	cd_statusdate=date()
	cd_receive=request("cd_receive")
	cd_pay=request("cd_pay")
	' cd_actreceive=request("cd_actreceive")
	' cd_actpay=request("cd_actpay")
	cd_post=request("cd_post")
	username=session("iId")
	cd_update=Now()
	cd_passno=request("cd_passno")
	cd_orderno=UCase(trim(request("cd_orderno")))
	cd_boxno=UCase(trim(request("cd_boxno")))
	cd_memo=request("cd_memo")
	if submit="提交" then
		'作业岗位2上如果无详细费用提交至4反之提交至3
		if not HasFee and cd_post=2 then cd_post=cd_post+1
		cd_post=cd_post+1
	end if
	if submit="退回" then
		'财务岗位4上如果无详细费用退回至2反之回退至3
		if not HasFee and cd_post=4 then cd_post=cd_post-1
		cd_post=cd_post-1
	end if
	if action="edit" then
		'如果是编辑状态，提交或保存按钮把所有信息更新
		if submit="保存" or submit="提交" then
			sql="update cd set wtr="&wtr&",bwtr="&bwtr&",category="&category&",cd_no='"&cd_no&"',cd_date='"&cd_date&"',cd_company='"&cd_company&"',cd_voyage='"&cd_voyage&"',cd_lading='"&cd_lading&"',cd_verify='"&cd_verify&"',cd_receive="&cd_receive&",cd_pay="&cd_pay&",cd_post="&cd_post&",cd_passno='"&cd_passno&"',cd_orderno='"&cd_orderno&"',cd_boxno='"&cd_boxno&"',cd_update='"&cd_update&"',cd_memo='"&cd_memo&"' where id="&id
			call ExecuteCommand(sql,false)
			'一旦进入编辑状态，先全部删除详细费用
			sql_delfee="delete * from fee where cd="&id
			call ExecuteCommand(sql_delfee,false)
			call AddFee()
		end if
		'如果是编辑状态，退回按钮只修改cd_post状态
		if submit="退回" then
			sql="update cd set cd_post="&cd_post&",cd_update='"&cd_update&"' where id="&id
			call ExecuteCommand(sql,false)
		end if
		'核销单管理只能修改核销单状态
		if submit="核销" then
			sql="update cd set cd_post="&cd_post&",cd_status="&cd_status&",cd_statusdate='"&cd_statusdate&"',cd_update='"&cd_update&"' where id="&id
			call ExecuteCommand(sql,false)
		end if
		if submit="已收" then
			cd_actreceive=request("cd_actreceive")
			sql_act="update cd set cd_received=true,cd_actreceive="&cd_actreceive&",cd_update='"&cd_update&"' where id="&id
			call ExecuteCommand(sql_act,false)
		end if
		if submit="已付" then
			cd_actpay=request("cd_actpay")
			sql_act="update cd set cd_paid=true,cd_actpay="&cd_actpay&",cd_update='"&cd_update&"' where id="&id
			call ExecuteCommand(sql_act,false)
		end if
	end if
	
	if action="add" then
		'如果是添加状态，新增和提交按钮起同样作用
		sql="insert into cd(wtr,bwtr,category,cd_no,cd_date,cd_company,cd_voyage,cd_lading,cd_verify,cd_receive,cd_pay,cd_post,username,cd_passno,cd_orderno,cd_boxno,cd_memo) values("&wtr&","&bwtr&","&category&",'"&cd_no&"','"&cd_date&"','"&cd_company&"','"&cd_voyage&"','"&cd_lading&"','"&cd_verify&"',"&cd_receive&","&cd_pay&","&cd_post&","&username&",'"&cd_passno&"','"&cd_orderno&"','"&cd_boxno&"','"&cd_memo&"')"
		'cdlastid=ExecuteCommand(sql,true)
		id=ExecuteCommand(sql,true)
		call AddFee()
	end if
	'response.write(cdlastid)
	'response.end
	
	function HasFee()
		HasFee=false
		c=request.form("detailid").count
		for i=1 to c
			detailid=request.form("detailid")(i)
			detailr=request.form("detailr_"&detailid)
			detailp=request.form("detailp_"&detailid)
			if IsNumberNz(detailr) or IsNumberNz(detailp) then
				HasFee=true
				exit function
			end if
		next
	end function
	
	function AddFee()
		c=request.form("detailid").count
		'response.write(c)
		for i=1 to c
			detailid=request.form("detailid")(i)
			detailr=request.form("detailr_"&detailid)
			detailp=request.form("detailp_"&detailid)
			'response.write(detailid&"空"&detailr&" "&detailp&"<br />")
			if IsNumberNz(detailr) or IsNumberNz(detailp) then
				if IsNumberNz(detailr) and IsNumberNz(detailp) then
					sql_addfee="insert into fee(cd,detail,receive,pay) values("&id&","&detailid&","&detailr&","&detailp&")"
				else
					if IsNumberNz(detailr) then sql_addfee="insert into fee(cd,detail,receive) values("&id&","&detailid&","&detailr&")"
					if IsNumberNz(detailp) then sql_addfee="insert into fee(cd,detail,pay) values("&id&","&detailid&","&detailp&")"
				end if
				call ExecuteCommand(sql_addfee,false)
			end if
		next
	end function
	
	'老的操作fee表函数，作废
	function OldOperateFee()
		bDelfee=false
		c=request.form("detailid").count
		'response.write(c)
		for i=1 to c
			detailid=request.form("detailid")(i)
			detailr=request.form("detailr_"&detailid)
			detailp=request.form("detailp_"&detailid)
			'response.write(detailid&"空"&detailr&" "&detailp&"<br />")
			if IsNumberNz(detailr) or IsNumberNz(detailp) then
				if action="edit" and not bDelfee then
					'一旦进入编辑状态，先全部删除详细费用
					sql_delfee="delete * from fee where cd="&id
					call ExecuteCommand(sql_delfee,false)
					bDelfee=true
				end if
				if IsNumberNz(detailr) and IsNumberNz(detailp) then
					sql_addfee="insert into fee(cd,detail,receive,pay) values("&id&","&detailid&","&detailr&","&detailp&")"
				else
					if IsNumberNz(detailr) then sql_addfee="insert into fee(cd,detail,receive) values("&id&","&detailid&","&detailr&")"
					if IsNumberNz(detailp) then sql_addfee="insert into fee(cd,detail,pay) values("&id&","&detailid&","&detailp&")"
				end if
				call ExecuteCommand(sql_addfee,false)
			end if
		next
	end function
	
	
	'事务提交
	' if conn.Errors.Count=0 then 
		' conn.CommitTrans  '如果没有conn错误，则执行事务提交
	' else
		' conn.RollbackTrans '否则回滚
	' end if

	if err.number=0 then 
		conn.CommitTrans  '如果没有conn错误，则执行事务提交
	else
		conn.RollbackTrans '否则回滚
		'回滚后的其他操作
		'strerr=err.Description
		url=request.ServerVariables("http_host")&request.ServerVariables("url")
		Response.Write "数据库错误！<br /><font color=red>Error # " & CStr(Err.Number) & "<br />" & Err.Description & "<br />Source: " & Err.Source &"<br />错误页面："&url&"</font><br />"
		Response.End
	end if
	
end if

if id<>"" then
	sql = "select * from cdfee where id="&id
	Set rs = Server.CreateObject("ADODB.Recordset")
	rs.Open sql,conn,1,1
	if not rs.eof then
		wtr=clng(rs("wtr"))
		bwtr=clng(rs("bwtr"))
		category=clng(rs("category"))
		cd_no=rs("cd_no")
		cd_date=rs("cd_date")
		cd_company=rs("cd_company")
		cd_voyage=rs("cd_voyage")
		cd_lading=rs("cd_lading")
		cd_verify=rs("cd_verify")
		cd_status=rs("cd_status")
		cd_statusdate=rs("cd_statusdate")
		cd_receive=rs("cd_receive")
		cd_pay=rs("cd_pay")
		cd_post=rs("cd_post")
		username=rs("username")
		cd_update=rs("cd_update")
		cd_passno=rs("cd_passno")
		cd_orderno=rs("cd_orderno")
		cd_boxno=rs("cd_boxno")
		cd_memo=rs("cd_memo")
		total_receive=rs("total_receive")
		total_pay=rs("total_pay")
		cd_actreceive=rs("cd_actreceive")
		cd_actpay=rs("cd_actpay")
		cd_received=rs("cd_received")
		cd_paid=rs("cd_paid")
	else
		'response.write("未找到数据")
		response.redirect("error.asp?id=1")
	end if
	rs.close
	set rs=nothing
end if
if not CheckPostFlag(session("aaPostflag"),cd_post,2) then call JumpErrorPage(2)
%>
<body>
<!--#include file="lib/header.asp"-->
<div id="main">
<!--#include file="lib/col1.asp"-->
	<div id="col3" role="main">
		<div id="col3_content" class="clearfix">
		<%
			' if not CheckPostFlag(session("aaPostflag"),cd_post,3) then
				' response.write("没有该岗位新增权限")
			' else
				' if not CheckPostFlag(session("aaPostflag"),cd_post,4) then
					' response.write("没有该岗位编辑权限")
				' else
		%>
			<form id="form1" name="form1" method="post" class="yform columnar" role="application">
				<fieldset>
					<legend>报关资料</legend>
					<div class="subcolumns">
						<div class="c33l">
							<div class="type-select">
								<label for="wtr">委托人*</label>
								<select name="wtr" id="wtr">
									<%
										Set rs_wtr = Server.CreateObject("ADODB.Recordset")
										sql = "SELECT * FROM wtr order by id"
										rs_wtr.Open sql,conn,1,1
										do while not rs_wtr.eof
									%>
									<option title="<%=rs_wtr("tmp")%>" value="<%=rs_wtr("id")%>" <%if rs_wtr("id")=wtr then response.write("selected='selected'")%>><%=rs_wtr("wtr")%></option>
									<%
											rs_wtr.movenext
										loop
										rs_wtr.close
										set rs_wtr=nothing
									%>
								</select>
							</div>
							<div class="type-text">
								<label for="cd_no">报关单号*</label>
								<input name="cd_no" id="cd_no" type="text" maxlength="18" class="validate[required,custom[onlyNumber],minSize[18],maxSize[18]]" value="<%if submit<>"新增" then response.write(cd_no)%>" />
							</div>
							<div class="type-text">
								<label for="cd_voyage">船名航次*</label>
								<input class="validate[required]" name="cd_voyage" id="cd_voyage" type="text" value="<%=cd_voyage%>" />
							</div>
							<div class="type-text">
								<label for="cd_passno">通关单号</label>
								<input class="validate[custom[onlyNumber],minSize[18],maxSize[18]]" name="cd_passno" id="cd_passno" type="text" maxlength="18" value="<%=cd_passno%>" />
							</div>
							<div class="type-text">
								<label for="cd_memo">备注</label>
								<textarea name="cd_memo" id="cd_memo"><%=cd_memo%></textarea>
							</div>
						</div>
						<div class="c33l">
							<div class="type-select">
								<label for="bwtr">被委托人*</label>
								<select name="bwtr" id="bwtr">
									<%
										Set rs_bwtr = Server.CreateObject("ADODB.Recordset")
										sql = "SELECT * FROM bwtr order by id"
										rs_bwtr.Open sql,conn,1,1
										do while not rs_bwtr.eof
									%>
									<option value="<%=rs_bwtr("id")%>" <%if rs_bwtr("id")=bwtr then response.write("selected='selected'")%>><%=rs_bwtr("bwtr")%></option>
									<%
											rs_bwtr.movenext
										loop
										rs_bwtr.close
										set rs_bwtr=nothing
									%>
								</select>
							</div>
							<div class="type-text">
								<label for="cd_date">申报日期*</label>
								<input class="datepicker" name="cd_date" id="cd_date" type="text" readonly="readonly" value="<%=cd_date%>" />
							</div>
							<div class="type-text">
								<label for="cd_lading">提单号*</label>
								<input class="validate[required,custom[onlyUcaseLetterNumber]]" name="cd_lading" id="cd_lading" type="text" value="<%=cd_lading%>" />
							</div>
							<div class="type-text">
								<label for="cd_orderno">订单号*</label>
								<input class="validate[required]" name="cd_orderno" id="cd_orderno" type="text" value="<%=cd_orderno%>" />
							</div>
						</div>
						<div class="c33r">
							<div class="type-select">
								<label for="category">类型*</label>
								<select name="category" id="category">
									<%
										Set rs_category = Server.CreateObject("ADODB.Recordset")
										sql = "SELECT * FROM category order by id"
										rs_category.Open sql,conn,1,1
										do while not rs_category.eof
									%>
									<option value="<%=rs_category("id")%>" <%if rs_category("id")=category then response.write("selected='selected'")%>><%=rs_category("category")%></option>
									<%
											rs_category.movenext
										loop
										rs_category.close
										set rs_category=nothing
									%>
								</select>
							</div>
							<div class="type-text">
								<label for="cd_company">企业名称*</label>
								<input class="validate[required]" name="cd_company" id="cd_company" type="text" value="<%=cd_company%>" />
							</div>
							<div class="type-text">
								<label for="cd_verify">核销单号</label>
								<input class="validate[custom[onlyNumber],minSize[9],maxSize[9]]" name="cd_verify" id="cd_verify" type="text" maxlength="9" value="<%=cd_verify%>" />
							</div>
							<div class="type-text">
								<label for="cd_boxno">箱号</label>
								<input class="validate[custom[onlyUcaseLetterNumber],minSize[11],maxSize[11]]" name="cd_boxno" id="cd_boxno" type="text" maxlength="11" value="<%=cd_boxno%>" />
							</div>
						</div>
					</div>
				</fieldset>
				<%if cd_post>1 and cd_post<5 then%>
				<fieldset>
					<legend>核销单状况</legend>
					<div class="subcolumns">
						<div class="c33l">
							<div class="type-check">
								<input name="cd_status" id="vote1" type="radio" value="1" <%if cd_status=1 or cd_status="" then response.write("checked='checked'")%> <%if cd_status=2 or cd_status=3 then response.write("disabled='disabled'")%> />
								<label for="vote1">未退</label>
							</div>
						</div>
						<div class="c33l">
							<div class="type-check">
								<input name="cd_status" id="vote2" type="radio" value="2" <%if cd_status=2 then response.write("checked='checked'")%> <%if cd_status=3 then response.write("disabled='disabled'")%> />
								<label for="vote2">海关已退</label>
								<span class="highlight"><%if cd_status=2 then response.write(cd_statusdate)%></span>
							</div>
						</div>
						<div class="c33r">
							<div class="type-check">
								<input name="cd_status" id="vote3" type="radio" value="3" <%if cd_status=3 then response.write("checked='checked'")%> />
								<label for="vote3">工厂已退</label>
								<span class="highlight"><%if cd_status=3 then response.write(cd_statusdate)%></span>
							</div>
						</div>
					</div>
				</fieldset>
				<%end if%>
				<fieldset>
					<legend>报关费</legend>
					<div class="subcolumns">
						<div class="c33l">
							<div class="type-select">
								<label for="tmp">模板*</label>
								<select name="tmp" id="tmp">
									<option value="0">模板&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;应收&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;应付</option>
								</select>
							</div>
							<div class="type-check">
								<input id="chk_detail" type="checkbox" <%if total_receive<>cd_receive or total_pay<>cd_pay then response.write("checked='checked'")%> />
								<label for="chk_detail">详细费用</label>
							</div>
						</div>
						<div class="c33l">
							<div class="type-text">
								<label for="cd_receive">应收*</label>
								<input class="validate[required,custom[number]]" name="cd_receive" id="cd_receive" type="text" value="<%=cd_receive%>" />
							</div>
						</div>
						<div class="c33r">
							<div class="type-text">
								<label for="cd_pay">应付*</label>
								<input class="validate[required,custom[number]]" name="cd_pay" id="cd_pay" type="text" value="<%=cd_pay%>" />
							</div>
						</div>
					</div>
				</fieldset>
				<%
					Set rs_calc = Server.CreateObject("ADODB.Recordset")
					sql = "SELECT * FROM catecount"
					rs_calc.Open sql,conn,1,1
					aaCate=rs_calc.getrows
					iCate=ubound(aaCate,1)
					rs_calc.close
					sql="select max(num) as row from(select count(feecategory) as num from detail group by feecategory)"
					rs_calc.Open sql,conn,1,1
					iRow=rs_calc("row")
					rs_calc.close
					'sql="select detail.id as detailid,detail.feecategory as cateid,detail.feename as detailname,detail.short as detailshort,iif(isnull(receive),0,receive) as detailreceive,iif(isnull(pay),0,pay) as detailpay,f.id as feeid from (SELECT * FROM fee where cd="
					sql="select detail.id as detailid,detail.feecategory as cateid,detail.feename as detailname,detail.short as detailshort,receive as detailreceive,pay as detailpay from (SELECT * FROM fee where cd="
					if id<>"" then
						sql=sql&id
					' elseif cdlastid<>""	then
						' sql=sql&cdlastid
					else
						sql=sql&"0"
					end if
					sql=sql&") as f right join detail on f.detail=detail.id order by detail.feecategory asc,detail.id asc"
					
					rs_calc.Open sql,conn,1,1
					aaFee=rs_calc.getrows
					rs_calc.close
					set rs_calc=nothing

				%>
				<table id="detail" class="full">
					<tbody>
						<tr>
						<%
							for i=0 to iCate
						%>							
							<th><%=aaCate(1,i)%></th>
							<th>应收</th>
							<th>应付</th>
						<%							
							next
						%>
						</tr>
						<tr>
						<%
							for i=0 to iCate
						%>							
							<th>合计</th>
							<th id="<%=aaCate(2,i)%>_receive"></th>
							<th id="<%=aaCate(2,i)%>_pay"></th>
						<%							
							next
						%>
						</tr>
					<%
						for i=0 to iRow-1
					%>
						<tr>
						<%
							for j=0 to iCate
						%>
							<td>
							<%
								'aaCate(0,j)=1 aaCate(3,j)=5
								if i\aaCate(3,j)>=1 then
									response.write("")
								else
									'aaFee(2,0)=码头费用
									n=0
									for z=0 to j-1
										n=n+aaCate(3,z)
									next
									n=n+i
									response.write(aaFee(2,n)&"<input type='hidden' name='detailid' value='"&aaFee(0,n)&"' />")
								end if
							%>
							</td>
							<td>
							<%
								'aaCate(0,j)=1 aaCate(3,j)=5
								if i\aaCate(3,j)>=1 then
									response.write("")
								else
									'aaFee(2,0)=码头费用
									n=0
									for z=0 to j-1
										n=n+aaCate(3,z)
									next
									n=n+i
									if aaFee(4,n)=0 then aaFee4r="" else aaFee4r=aaFee(4,n)
									response.write("<input type='text' size='3' id='detailr_"&aaFee(0,n)&"' class='validate[custom[number]]' name='detailr_"&aaFee(0,n)&"' value='"&aaFee4r&"' tabindex='"&aaFee(0,n)&"' />")
								end if
							%>
							</td>
							<td>
							<%
								'aaCate(0,j)=1 aaCate(3,j)=5
								if i\aaCate(3,j)>=1 then
									response.write("")
								else
									'aaFee(2,0)=码头费用
									n=0
									for z=0 to j-1
										n=n+aaCate(3,z)
									next
									n=n+i
									if aaFee(5,n)=0 then aaFee5p="" else aaFee5p=aaFee(5,n)
									response.write("<input type='text' size='3' id='detailp_"&aaFee(0,n)&"' class='validate[custom[number]]' name='detailp_"&aaFee(0,n)&"' value='"&aaFee5p&"' tabindex='"&aaFee(0,n)&"' />")
								end if
							%>
							</td>
						<%							
							next
						%>
						</tr>	
					<%							
						next
					%>
					</tbody>
				</table>
				<div class="error">
					<div class="subcolumns">
						<div class="c25l">
							<div class="type-text">
								<label for="total_receive">合计应收*</label>
								<input name="total_receive" id="total_receive" type="text" readonly="readonly" value="<%=total_receive%>" />
							</div>
						</div>
						<div class="c25l">
							<div class="type-text">
				<%if action="edit" and cd_post=4 and CheckPostFlag(session("aaPostflag"),4,4) then%>
								<label for="cd_actreceive">实际已收*</label>
								<input class="validate[required,custom[number]]" name="cd_actreceive" id="cd_actreceive" type="text" value="<%=cd_actreceive%>" />
				<%end if%>
							</div>
						</div>
						<div class="c25l">
							<div class="type-text">
								<label for="total_pay">合计应付*</label>
								<input name="total_pay" id="total_pay" type="text" readonly="readonly" value="<%=total_pay%>" />
							</div>
						</div>
						<div class="c25r">
							<div class="type-text">
				<%if action="edit" and cd_post=4 and CheckPostFlag(session("aaPostflag"),4,4) then%>
								<label for="cd_actpay">实际已付*</label>
								<input class="validate[required,custom[number]]" name="cd_actpay" id="cd_actpay" type="text" value="<%=cd_actpay%>" />
				<%end if%>
							</div>
						</div>
					</div>
				</div>
				<div class="type-button">
					<input id="id" type="hidden" value="<%=id%>" />
					<input id="action" name="action" type="hidden" value="<%=action%>" />
					<input id="cd_post" name="cd_post" type="hidden" value="<%=cd_post%>" />
				<%if action="add" and CheckPostFlag(session("aaPostflag"),1,3) then%>
					<input type="submit" id="submit_add" name="submit" value="新增" />
				<%end if%>
				<%if action="edit" and cd_post<>4 and (not (cd_received or cd_paid)) and CheckPostFlag(session("aaPostflag"),cd_post,4) then%>
					<input type="submit" name="submit" value="保存" />
				<%end if%>
				<%if (cd_post>=1 and cd_post<4) and CheckPostFlag(session("aaPostflag"),cd_post,3) then%>
					<input type="submit" id="submit_next" name="submit" value="提交" />
				<%end if%>
				<%if action="edit" and (cd_post>1 and cd_post<5) and (not (cd_received or cd_paid)) and CheckPostFlag(session("aaPostflag"),cd_post,4) then%>
					<input type="submit" name="submit" value="退回" />
				<%end if%>
				<%if action="edit" and not cd_received and cd_post=4 and CheckPostFlag(session("aaPostflag"),4,4) then%>
					<input type="submit" name="submit" value="已收" />
				<%end if%>
				<%if action="edit" and not cd_paid and cd_post=4 and CheckPostFlag(session("aaPostflag"),4,4) then%>
					<input type="submit" name="submit" value="已付" />
				<%end if%>
				<%if action="edit" and cd_post>1 and cd_post<5 and CheckPostFlag(session("aaPostflag"),5,4) and cd_status<3 then%>
					<input type="submit" name="submit" value="核销" />
				<%end if%>
				<%if action="edit" and CheckPostFlag(session("aaPostflag"),cd_post,2) then%>
					<input type="button" id="print_process" value="打印" />
				<%end if%>
				</div>
			</form>
			
			
			<table id="cd" class="full">
				<thead>
					<tr><th colspan="12" scope="col">添加情况</th></tr>
				</thead>
				<tbody>
					<tr>
						<th scope="col">报关单号</th>
						<th scope="col">委托人</th>
						<th scope="col">被委托人</th>
						<th scope="col">类型</th>
						<th scope="col">申报日期</th>
						<th scope="col">企业名称</th>
						<th scope="col">船名/航次</th>
						<th scope="col">提单号</th>
						<th scope="col">核销单号</th>
						<th scope="col">更新时间</th>
						<th scope="col">岗位</th>
						<th scope="col">备注</th>
					</tr>
					<%
						Set rs_new = Server.CreateObject("ADODB.Recordset")
						sql = "SELECT * FROM cdfee where username=1 and DateValue(cd_update)=date() order by cd_update desc"
						rs_new.Open sql,conn,1,1
						do while not rs_new.eof
					%>
					<tr>
						<th class="sub" scope="row"><a href="process.asp?id=<%=rs_new("id")%>"><%=right(rs_new("cd_no"),9)%></a></th>
						<td><%=rs_new("wtr_name")%></td>
						<td><%=rs_new("bwtr_name")%></td>
						<td><%=rs_new("category_name")%></td>
						<td><%=rs_new("cd_date")%></td>
						<td><%=rs_new("cd_company")%></td>
						<td><%=rs_new("cd_voyage")%></td>
						<td><%=rs_new("cd_lading")%></td>
						<td><%=rs_new("cd_verify")%></td>
						<td><%=rs_new("cd_update")%></td>
						<td><%=GetPostString(rs_new("cd_post"))%></td>
						<td><%=rs_new("cd_memo")%></td>
					</tr>
					<%
							rs_new.movenext
						loop
						rs_new.close
						set rs_new=nothing
					%>
				</tbody>
			</table>			
		<%
				' end if
			' end if
		%>	
		</div>
	</div>
</div>
<!--#include file="lib/footer.asp"-->
</body>
</html>
<!--#include file="lib/connclose.asp"-->

